package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.common.Code;
import com.ruan.common.Result;
import com.ruan.mapper.*;
import com.ruan.pojo.*;
import com.ruan.pojoExt.EmployeeExt;
import com.ruan.service.DepartmentService;
import com.ruan.service.EmployeeService;
import com.ruan.service.PositionService;
import com.ruan.service.ProjectToolAssignmentService;
//import com.ruan.utils.PoiUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Resource
    private DepartmentService departmentService;

    @Autowired
    private EmployeeLeaveMapper employeeLeaveMapper;//请假

    @Autowired
    private BusinessTripMapper businessTripMapper;//出差

    @Autowired
    private OverTimeMapper overTimeMapper;//加班

    @Autowired
    private OnDutyMapper onDutyMapper;//出勤

    @Autowired
    private ResignMapper resignMapper;//离职

    @Autowired
    private MonthCountMapper monthCountMapper;//月统计

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private CommonUserMapper commonUserMapper;

    @Autowired
    private SalaryMapper salaryMapper;

    @Autowired
    private SalarySetMapper salarySetMapper;

    @Autowired
    private PositionService positionService;

    @Autowired
    private ProjectAssignmentsMapper projectAssignmentsMapper;

    @Autowired
    private ParticipantMapper participantMapper;

    @Autowired
    private ProjectToolAssignmentService projectToolAssignmentService;

    @Autowired
    private ProjectToolAssignmentMapper projectToolAssignmentMapper;

    @Autowired
    private ToolBorrowRecordsMapper toolBorrowRecordsMapper;

    @Autowired
    private TasksMapper tasksMapper;

    @Autowired
    private HttpServletResponse response;

    @Value("${kejiu.path}")//必须是org.springframework.beans.factory.annotation提供的Value
    private String basePath;

    /**
     * 将员工信息导出为Excel
     * @throws Exception
     * @return
     */
    @Override
//    public void exportToExcel() throws Exception{
    public byte[] exportToExcel() {
        List<Employee> employeeList = employeeMapper.selectList(null);
//        为在调用PoiUtils类的exportEmployeeInfo方法时，该方法是非静态方法，
//        而你是通过类名直接调用的，需要先创建PoiUtils对象再调用该方法。
//        PoiUtils poiUtils = new PoiUtils();
//        poiUtils.exportEmployeeInfo(employeeList);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        XSSFWorkbook excel=new XSSFWorkbook(); //工作簿，允许您在Java应用程序中创建、读取和修改Excel文件
        //创建一个工作表对象
        XSSFSheet sheet = excel.createSheet("sheet1");
        //在工作表中创建行对象
////        XSSFRow row1 = sheet.createRow(0); //创建第一行表头
////        row1.createCell(0).setCellValue("姓名");//创建第一行第一个单元格
////        row1.createCell(1).setCellValue("性别");//创建第一行第二个单元格
////        row1.createCell(2).setCellValue("手机号");//创建第一行第三个单元格
////        row1.createCell(3).setCellValue("邮箱");//创建第一行第四个单元格
////        row1.createCell(4).setCellValue("出生日期");//创建第一行第五个单元格
////        row1.createCell(5).setCellValue("家庭住址");//创建第一行第六个单元格
////        row1.createCell(6).setCellValue("现住址");//创建第一行第七个单元格
////        row1.createCell(7).setCellValue("学历");//创建第一行第八个单元格
////        row1.createCell(8).setCellValue("毕业院校");//创建第一行第九个单元格
////        row1.createCell(9).setCellValue("所在部门");//创建第一行第十个单元格
////        row1.createCell(10).setCellValue("职位");//创建第十一行第四个单元格
////        row1.createCell(11).setCellValue("入职时间");//创建第十一行第四个单元格
////        row1.createCell(12).setCellValue("离职时间");//创建第十一行第四个单元格
////        row1.createCell(13).setCellValue("状态");//创建第十一行第四个单元格

        String[] headers = {"姓名", "性别", "手机号",
                "邮箱", "出生日期", "省",
                "市", "区","详细地址", "学历", "毕业院校",
                "所在部门", "职位", "入职时间",
                "离职时间", "状态"};

        XSSFRow row1 = sheet.createRow(0); // Create the first row for headers

        for (int i = 0; i < headers.length; i++) {
            row1.createCell(i).setCellValue(headers[i]); // Create cell for each header
        }

        for (int i = 0; i < employeeList.size(); i++) {
            Employee employee = employeeList.get(i);
            Date birth = employee.getBirth();
            Date workTime = employee.getWorktime();
            Date outTime = employee.getOuttime();
            XSSFRow row2 = sheet.createRow(i+1);
//
//
////            row2.createCell(0).setCellValue(employee.getName());//创建第二行第一个单元格
////            row2.createCell(1).setCellValue(employee.getGender());//创建第二行第二个单元格
////            row2.createCell(2).setCellValue(employee.getPhone());//创建第二行第三个单元格
////            row2.createCell(3).setCellValue(employee.getEmail());//创建第二行第三个单元格
////            row2.createCell(4).setCellValue(sdf.format(birth));//创建第二行第三个单元格
////            row2.createCell(5).setCellValue(employee.getHome());//创建第二行第三个单元格
////            row2.createCell(6).setCellValue(employee.getAddress());//创建第二行第三个单元格
////            row2.createCell(7).setCellValue(employee.getEducation());//创建第二行第三个单元格
////            row2.createCell(8).setCellValue(employee.getSchool());//创建第二行第三个单元格
////            row2.createCell(9).setCellValue(employee.getDepartment());//创建第二行第三个单元格
////            row2.createCell(10).setCellValue(employee.getPosition());//创建第二行第三个单元格
////            row2.createCell(11).setCellValue(sdf.format(worktime));//创建第二行第三个单元格
            if(employee != null){//departmentId
//                Long departmentId = employee.getDepartmentId();
//                Long positionId = employee.getPositionId();
//                System.out.println("departmentId是:" + departmentId);
//                System.out.println("positionId是:" + positionId);
                Department department = new Department();
                Position position = new Position();
//
                if(employee != null && employee.getPositionId() != null) {
                    System.out.println("positionService是空的："+positionService);
                    position = positionService.getById(employee.getPositionId());
                    System.out.println("position是：" + position);
                }

                if(employee != null && employee.getDepartmentId() != null) {
                    System.out.println("departmentService是空的："+departmentService);
                    System.out.println("departmentMapper是空的："+departmentMapper);
                    department = departmentService.getById(employee.getDepartmentId());//报空指针。。。。。。啊啊啊啊
                    System.out.println("department是：" + department);
                }

                String[] employeeInfo = {
                        employee.getName(), employee.getGender(),
                        employee.getPhone(), employee.getEmail(),
                        sdf.format(birth), employee.getProvinceName(),
                        employee.getCityName(), employee.getDistrictName(),
                        employee.getDetail(),employee.getEducation(),
                        employee.getSchool(), department.getName(),
                        position.getPname(), sdf.format(workTime),
                        outTime != null ? sdf.format(outTime) : "",
                         employee.getStatus()
                };

                for (int j = 0; j < employeeInfo.length; j++) {
                    row2.createCell(j).setCellValue(employeeInfo[j]);
                }
            }
        }

        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
        header.setContentDispositionFormData("attachment", "员工表.xlsx");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            excel.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outputStream.toByteArray();
//        return ResponseEntity.ok()
//                .headers(header)
//                .body(outputStream.toByteArray());

//        String fileName = "员工表" + UUID.randomUUID().toString() + ".xlsx";
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
//        excel.write(response.getOutputStream());
//        excel.close();

        //创建一个输出流，通过输出流将内存中的Excel文件写到磁盘
//        String fileName = "员工表" + UUID.randomUUID().toString()+".xlsx";
//        String filePath = basePath+"Excel文件/"+fileName;
////        String filePath = "D:/DLSS/Excel文件/"+fileName;
////        String filePath = "D://Employee_Info.xlsx";
//
////        String fileName = "Employee_Info.xlsx";
//        OutputStream outputStream=new FileOutputStream(new File(filePath));
//        excel.write(outputStream);
//        outputStream.flush();
//        excel.close();
//        QiNiuUtils.uploadToQiNiu(filePath,fileName);

    }

    /**
     * 获得所有员工信息
     * @return
     */
    @Override
    public Result getEmployeeInfo() {
        //employeeService 是用来处理业务逻辑的服务层，
        // 而 employeeMapper 则是用来与数据库进行交互的数据访问层。
//        List<Employee> employeeList = employeeMapper.selectAll();
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.notIn(Employee::getStatus, "待审核", "离职");
        //如果没有设置条件，就是获取整张表的数据
        List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
        List<EmployeeExt> employeeExtList = new ArrayList<>();
        for(Employee employee : employeeList){
            EmployeeExt employeeExt = new EmployeeExt();
            BeanUtils.copyProperties(employee,employeeExt);
            Department department = departmentService.getById(employee.getDepartmentId());
            if(department != null) {
                employeeExt.setDepartmentName(department.getName());
            }
            Position position = positionService.getById(employee.getPositionId());
            if(position != null) {
                employeeExt.setPosition(position.getPname());
            }
            employeeExtList.add(employeeExt);
        }
        System.out.println("员工信息列表为："+employeeExtList);
        return new Result(Code.SELECT_OK,employeeExtList,"员工信息查询成功");
    }

    /**
     * 获得当前部门所有员工信息
     * @return
     */
    @Override
    public Result getCurrentDepartEmployeeInfo(Integer departId) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.notIn(Employee::getStatus, "待审核", "离职");
        queryWrapper.eq(Employee::getDepartmentId,departId);
        //如果没有设置条件，就是获取整张表的数据
        List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
        List<EmployeeExt> employeeExtList = new ArrayList<>();
        for(Employee employee : employeeList){
            EmployeeExt employeeExt = new EmployeeExt();
            BeanUtils.copyProperties(employee,employeeExt);
            Department department = departmentService.getById(employee.getDepartmentId());
            if(department != null) {
                employeeExt.setDepartmentName(department.getName());
            }
            Position position = positionService.getById(employee.getPositionId());
            if(position != null) {
                employeeExt.setPosition(position.getPname());
            }
            employeeExtList.add(employeeExt);
        }
        System.out.println("该部门所有员工信息列表为："+employeeExtList);
        return new Result(Code.SELECT_OK,employeeExtList,"部门的员工信息查询成功");
    }

    /**
     * 实习员工转正操作
     * @param id 员工id
     * @return
     */
    @Override
    public Boolean becomeFullTime(Integer id) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Employee::getId,id);
        Employee employee = employeeMapper.selectById(id);
        if (employee!=null){
            employee.setStatus("正式");
            if (employeeMapper.update(employee, queryWrapper)>0){
                return true;
            }
        }
        return false;
    }

    /**
     * 辞退正式员工操作
     * @param id 员工id
     * @return
     */
    @Override
    public Boolean dismissedEmployee(Integer id) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<EmployeeLeave> leaveWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<BusinessTrip> tripWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<OverTime> overTimeWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<OnDuty> onDutyWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<Resign> resignWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<MonthCount> monthCountWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<Salary> salaryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SalarySet> salarySetWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ToolBorrowRecords> toolBorrowRecordsWrapper = new LambdaQueryWrapper<>();

        LambdaQueryWrapper<ProjectAssignments> projectAssignmentsWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Participant> participantWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Tasks> tasksWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Employee::getId,id);
        Employee employee = employeeMapper.selectById(id);
        if (employee!=null){
            employee.setStatus("离职");
            long d = System.currentTimeMillis();
            Date date = new Date(d);
            employee.setOuttime(date);//设置离职时间
            if (employeeMapper.updateById(employee)>0){
                LambdaQueryWrapper<CommonUser> commonUserWrapper = new LambdaQueryWrapper<>();

                commonUserWrapper.eq(CommonUser::getPhone,employee.getPhone());
                CommonUser commonUser = commonUserMapper.selectOne(commonUserWrapper);

//                if (commonUser != null){
//                    commonUser.setStatus("禁用");
//                    commonUserMapper.updateById(commonUser);//离职后禁用员工账号
//                }else {
//                    throw new SystemException(Code.BUSINESS_ERR,"此员工账号查找失败！");
//                }
                //删除账号
//                List<CommonUser> commonUserList = commonUserMapper.selectList(commonUserWrapper);
//                if (commonUserList.size()>0){
//                    commonUserMapper.delete(commonUserWrapper);//离职后删除对应的账号
//                }

                if (commonUser != null){
                    commonUser.setStatus("禁用");
                    commonUserMapper.updateById(commonUser);//离职后禁用员工账号
                }

                leaveWrapper.eq(EmployeeLeave::getEmployeeId,employee.getId());
                tripWrapper.eq(BusinessTrip::getEmployeeId,employee.getId());
                overTimeWrapper.eq(OverTime::getEmployeeId,employee.getId());
                onDutyWrapper.eq(OnDuty::getEmployeeId,employee.getId());
                resignWrapper.eq(Resign::getEmployeeId,employee.getId());
                monthCountWrapper.eq(MonthCount::getEmployeeId,employee.getId());
                salaryWrapper.eq(Salary::getEmployeeId,employee.getId());
                salarySetWrapper.eq(SalarySet::getEmployeeId,employee.getId());
                toolBorrowRecordsWrapper.eq(ToolBorrowRecords::getEmployeeId,employee.getId());
                projectAssignmentsWrapper.eq(ProjectAssignments::getEmployeeId,employee.getId());
                participantWrapper.eq(Participant::getEmployeeId,employee.getId());
                tasksWrapper.eq(Tasks::getEmployeeId,employee.getId());

                List<EmployeeLeave> employeeLeaveList = employeeLeaveMapper.selectList(leaveWrapper);
                if (employeeLeaveList.size()>0){
                    employeeLeaveMapper.delete(leaveWrapper);//离职后删除对应的请假记录
                }

                List<BusinessTrip> tripList = businessTripMapper.selectList(tripWrapper);
                if (tripList.size()>0){
                    businessTripMapper.delete(tripWrapper);//离职后删除对应的出差记录
                }

                List<OverTime> overTimeList = overTimeMapper.selectList(overTimeWrapper);
                if (overTimeList.size()>0){
                    overTimeMapper.delete(overTimeWrapper);//离职后删除对应的加班记录
                }

                List<OnDuty> onDutyList = onDutyMapper.selectList(onDutyWrapper);
                if (onDutyList.size()>0){
                    onDutyMapper.delete(onDutyWrapper);//离职后删除对应的出勤记录
                }

                List<Resign> resignList = resignMapper.selectList(resignWrapper);
                if (resignList.size()>0){
                    resignMapper.delete(resignWrapper);//离职后删除对应的离职记录
                }

                List<MonthCount> monthCountList = monthCountMapper.selectList(monthCountWrapper);
                if (monthCountList.size()>0){
                    monthCountMapper.delete(monthCountWrapper);//离职后删除对应的月统计记录
                }
                List<Salary> salaryList = salaryMapper.selectList(salaryWrapper);
                if (salaryList.size()>0){
                    salaryMapper.delete(salaryWrapper);//离职后删除对应的薪资记录
                }

                List<SalarySet> salarySetList = salarySetMapper.selectList(salarySetWrapper);
                if (salarySetList.size()>0){
                    salarySetMapper.delete(salarySetWrapper);//离职后删除对应的薪资设置
                }

                List<ToolBorrowRecords> toolBorrowRecordsList = toolBorrowRecordsMapper.selectList(toolBorrowRecordsWrapper);
                if (toolBorrowRecordsList.size()>0){
                    toolBorrowRecordsMapper.delete(toolBorrowRecordsWrapper);//离职后删除对应的工具借用记录
                }

                List<ProjectAssignments> projectAssignmentsList = projectAssignmentsMapper.selectList(projectAssignmentsWrapper);
                if (projectAssignmentsList.size()>0){
                    //删除这个项目所用到的工具信息
                    for(ProjectAssignments projectAssignments : projectAssignmentsList){
                        LambdaQueryWrapper<ProjectToolAssignment> projectToolAssignmentWarpper = new LambdaQueryWrapper<>();
                        projectToolAssignmentWarpper.eq(ProjectToolAssignment::getProjectId,projectAssignments.getProjectId());
                        List<ProjectToolAssignment> projectToolAssignmentList = projectToolAssignmentMapper.selectList(projectToolAssignmentWarpper);
                        if (projectToolAssignmentList.size()>0) {
                            projectToolAssignmentMapper.delete(projectToolAssignmentWarpper);
                        }
                    }
                    projectAssignmentsMapper.delete(projectAssignmentsWrapper);//离职后删除对应的项目分配信息
                }

                List<Participant> participantList = participantMapper.selectList(participantWrapper);
                if (participantList.size()>0){
                    participantMapper.delete(participantWrapper);//离职后删除对应人员的项目参与信息
                }

                List<Tasks> tasksList = tasksMapper.selectList(tasksWrapper);
                if (tasksList.size()>0){
                    tasksMapper.delete(tasksWrapper);//离职后删除对应人员的所有任务信息
                }

                Long departId = employee.getDepartmentId();
                Department department = departmentMapper.selectById(departId);
                department.setNumber(department.getNumber()-1);//部门的人数减一
                departmentMapper.updateById(department);
                return true;
            }
        }
        return false;
    }

    //条件搜索员工
    //queryEmployeeCondition 查询条件
    @Override
    public List<Employee> searchEmployeeByCondition(QueryEmployeeCondition queryEmployeeCondition) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(queryEmployeeCondition.getDepartmentId()!=null,Employee::getDepartmentId,queryEmployeeCondition.getDepartmentId());
        queryWrapper.eq(queryEmployeeCondition.getStatus()!=null,Employee::getStatus,queryEmployeeCondition.getStatus());
        queryWrapper.like(queryEmployeeCondition.getName()!=null,Employee::getName,queryEmployeeCondition.getName());
        List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
        if (employeeList!=null){
            return employeeList;
        }else {
            return null;
        }
    }


    //更改员工信息
    @Override
    public Boolean updateEmployee(Employee employee) {

        Long id = employee.getId();
        Employee employeeOld = employeeMapper.selectById(id);//更改前原员工信息

        Long departmentOld = employeeOld.getDepartmentId();//更改前员工的部门id
        Long departmentId = employee.getDepartmentId();//更改后员工的部门id

        Long positionOldId = employeeOld.getPositionId();
        Position position1 = positionService.getById(positionOldId);
        String positionOld = position1.getPname();//更改前员工职位
        
        Long positionId = employee.getPositionId();
        Position position = positionService.getById(positionId);//更改后的员工职位

        System.out.println("更改前的职位："+positionOld);
        System.out.println("更改后的职位："+position);
        
        Department departmentBefor = departmentMapper.selectById(departmentOld);//更改之前的部门
        Department departmentAfter = departmentMapper.selectById(departmentId);//更改前之后的部门

        System.out.println("更改前的部门："+departmentBefor);
        System.out.println("更改后的部门："+departmentAfter);

       if (position.getPname().equals(positionOld) && departmentId.equals(departmentOld)){
            //部门和职位未发生变化//更新其它属性
           System.out.println("部门和职位未发生变化");
           return employeeMapper.updateById(employee) > 0;
        }
        if (employeeOld.getStatus().equals("实习") && !position.getPname().equals(positionOld)){
            System.out.println("实习期间不能换职位");
            return false;//实习期间不能换职位
        }
        //更改前为总经理 副总经理 总监 副总监(降职)
        if (positionOld.equals("总经理")
                || positionOld.equals("副总经理")
                || positionOld.equals("总监")
                || positionOld.equals("副总监")){
            System.out.println("更改前为总经理 副总经理 总监 副总监(降职)");
            //降为员工，直接更新员工信息
            if (position.getPname().equals("员工")) {
                System.out.println("降为员工，直接更新员工信息");
//                departmentAfter.setNumber(departmentAfter.getNumber()+1);
//                departmentMapper.updateById(departmentAfter);
                return employeeMapper.updateById(employee) > 0;
            }else {//降后不是员工
                System.out.println("降后不是员工");
                //降为总经理 副总经理 总监 副总监，需要判断该部门是否存在这些职位
                if (position.getPname().equals("总经理") ||
                    position.getPname().equals("副总经理") ||
                    position.getPname().equals("总监") ||
                    position.getPname().equals("副总监")){
                    System.out.println("降为总经理 副总经理 总监 副总监，需要判断该部门是否存在这些职位");
                    LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper1.eq(Department::getId,departmentId);
                    Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);
                    lambdaQueryWrapper.eq(Employee::getDepartmentId,department1.getId());
                    lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                    lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                    Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                    if (employeeQuery!=null){//该部门该职位已经有人了，返回false
                        return false;
                    }else {
                        return employeeMapper.updateById(employee) > 0;
                    }
                }else {
                    //降为部门经理 部门副经理 部门主管 部门副主管，需要判断该部门是否存在这些职位
                    System.out.println("降为部门经理 部门副经理 部门主管 部门副主管，需要判断该部门是否存在这些职位");
                    LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper1.eq(Department::getId,departmentId);
                    Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);
                    lambdaQueryWrapper.eq(Employee::getDepartmentId,department1.getId());
                    lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                    lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                    Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                    if (employeeQuery!=null){//该部门该职位存在，返回false
                        return false;
                    }else {//该部门该职位不存在//且降职为部门经理(需要单独讨论)
                        if (position.getPname().equals("部门经理")){
                            System.out.println("如果是该部门的部门经理，需要修改该部门的负责人和联系电话");
                            //如果是该部门的部门经理，需要修改该部门的负责人和联系电话
                            departmentAfter.setEmployeeId(employee.getId());
                            departmentAfter.setPhone(employee.getPhone());
//                            departmentAfter.setNumber(departmentAfter.getNumber()+1);
                            departmentMapper.updateById(departmentAfter);
                            return employeeMapper.updateById(employee) > 0;
                        }else {//该部门该职位不存在//且降职后不是该部门的部门经理
                            System.out.println("该部门该职位不存在//且降职后不是该部门的部门经理");
//                            departmentAfter.setNumber(departmentAfter.getNumber()+1);
//                            departmentMapper.updateById(departmentAfter);
                            return employeeMapper.updateById(employee) > 0;
                        }
                    }
                }
            }
        }
        //更改前是部门经理
        if (positionOld.equals("部门经理")){
            System.out.println("更改前是部门经理");
            if (position.getPname().equals("员工")) { //更改后为员工，需要修改该部门的负责人和联系电话，
                System.out.println("更改后为员工，需要修改该部门的负责人和联系电话，");
                //员工可以有多个
                if (departmentId.equals(departmentOld)){ //更改后还是本部门的员工,需要修改该部门的负责人和联系电话为null，
                    System.out.println("更改后还是本部门的员工,需要修改该部门的负责人和联系电话");
                    //部门经理清空，待重新安排
//                    departmentBefor.setEmployeeId(null);
//                    departmentBefor.setPhone("暂无");
//                    departmentMapper.updateById(departmentBefor);
                    Department department = departmentService.lambdaQuery().eq(Department::getId,departmentBefor.getId()).one();
                    if(department!=null){
                        System.out.println("department是"+department);
                        departmentService.update(new UpdateWrapper<Department>().lambda()
                                .set(Department::getPhone,"暂无")
                                .set(Department::getEmployeeId,null)
                                .eq(Department::getId,department.getId()));
                        System.out.println("departmentBefor是"+departmentService.getById(departmentBefor.getId()));
                    }
                    return employeeMapper.updateById(employee) > 0;
                }else { //更改后不是本部门的员工，原来部门人数减一，修改后的部门人数加一
                    System.out.println("更改后不是本部门的员工");
//                    departmentBefor.setNumber(departmentBefor.getNumber() - 1);
//                    departmentMapper.updateById(departmentBefor);
//                    departmentAfter.setNumber(departmentAfter.getNumber()+1);
//                    departmentMapper.updateById(departmentAfter);
                    return employeeMapper.updateById(employee) > 0;
                }
            }else {//更改后不是员工
                // 升职为为总经理 副总经理 总监 副总监,判断是否已存在(只能有一个)
                if (position.getPname().equals("总经理") ||
                        position.getPname().equals("副总经理") ||
                        position.getPname().equals("总监") ||
                        position.getPname().equals("副总监")) {
                    System.out.println("升职为为总经理 副总经理 总监 副总监");
                    LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    //-----------------------------------------
                    LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper1.eq(Department::getId,departmentId);
                    Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);//getByName
                    if(department1!=null){
                        lambdaQueryWrapper.eq(department1.getId()!=null,
                                Employee::getDepartmentId,department1.getId());
                        lambdaQueryWrapper.eq(position.getId()!=null,
                                Employee::getPositionId,position.getId());
                    }
                    lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                    Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                    if (employeeQuery != null) {//该职位存在，返回false
                        return false;
                    } else {//该职位不存在，该员工原来所在部门人数减一，需要修改该部门的负责人和联系电话为暂无
//                        departmentBefor.setEmployeeId(null);
//                        departmentBefor.setPhone("暂无");
//                        departmentBefor.setNumber(departmentBefor.getNumber() - 1);
//                        departmentMapper.updateById(departmentBefor);
                        Department department = departmentService.lambdaQuery().eq(Department::getId,departmentBefor.getId()).one();
                        if(department!=null){
                            System.out.println("department是"+department);
                            departmentService.update(new UpdateWrapper<Department>().lambda()
                                    .set(Department::getPhone,"暂无")
                                    .set(Department::getEmployeeId,null)
                                    .eq(Department::getId,department.getId()));
                            System.out.println("departmentBefor是"+departmentService.getById(departmentBefor.getId()));
                        }
                        return employeeMapper.updateById(employee) > 0;
                    }
                } else {
                    System.out.println("降职为部门主管 部门副主管 部门副经理");
                    //降职为部门主管 部门副主管 部门副经理，需要判断该部门是否存在这些职位
                    //依旧在本部门
                    if (departmentId.equals(departmentOld)) {
                        System.out.println("依旧在本部门");
                        if (position.getPname().equals(positionOld)) {
                            System.out.println("如果还是本部门的部门经理");
                            //如果还是本部门的部门经理（也就是未修改），返回true
                            return true;
                        } else {
                            //如果是降职为本部门的副经理 部门主管 部门副主管，判断是否存在
                            System.out.println("降职为本部门的副经理 部门主管 部门副主管");
                            LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                            LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                            lambdaQueryWrapper1.eq(Department::getId,departmentId);
                            Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);//getByName
                            lambdaQueryWrapper.eq(Employee::getDepartmentId,department1.getId());
                            lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                            lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                            Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                            if (employeeQuery != null) {//该职位存在，返回false
                                return false;
                            } else { //该职位不存在，降职，需要修改该部门的负责人和联系电话为暂无
//                                departmentBefor.setEmployeeId(null);
//                                departmentBefor.setPhone("暂无");
//                                departmentMapper.updateById(departmentBefor);
                                Department department = departmentService.lambdaQuery().eq(Department::getId,departmentBefor.getId()).one();
                                if(department!=null){
                                    System.out.println("department是"+department);
                                    departmentService.update(new UpdateWrapper<Department>().lambda()
                                            .set(Department::getPhone,"暂无")
                                            .set(Department::getEmployeeId,null)
                                            .eq(Department::getId,department.getId()));
                                    System.out.println("departmentBefor是"+departmentService.getById(departmentBefor.getId()));
                                }
                                return employeeMapper.updateById(employee) > 0;
                            }
                        }
                    //调到其他部门
                    } else {
                        System.out.println("调到其他部门");
                        if (position.getPname().equals(positionOld)) { //调到别的部门当部门经理
                            System.out.println("调到别的部门当部门经理");
                            LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                            LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                            lambdaQueryWrapper1.eq(Department::getId,departmentId);
                            Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);//getByName
                            lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                            lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                            lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                            Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                            if (employeeQuery != null) {//该部门的部门经理存在，返回false
                                return false;
                            } else {
                                System.out.println("调到别的部门当部门经理,该部门的部门经理不存在");
                                //该部门的部门经理不存在，原来部门的负责人和联系电话为暂无且人数减一，
                                // 修改新部门的负责人和联系电话且人数加一，并修改员工信息
//                                departmentBefor.setEmployeeId(null);//写null时，不更新
//                                departmentBefor.setPhone("暂无");//"暂无"
//                                System.out.println("departmentBefor是"+departmentBefor);
//                                departmentBefor.setNumber(departmentBefor.getNumber() - 1);
//                                departmentService.updateById(departmentBefor);

                                Department department = departmentService.lambdaQuery().eq(Department::getId,departmentBefor.getId()).one();
                                if(department!=null){
                                    System.out.println("department是"+department);
                                    departmentService.update(new UpdateWrapper<Department>().lambda()
                                            .set(Department::getPhone,"暂无")
                                            .set(Department::getEmployeeId,null)
                                            .eq(Department::getId,department.getId()));
                                    System.out.println("departmentBefor是"+departmentService.getById(departmentBefor.getId()));
                                }

                                departmentAfter.setEmployeeId(employee.getId());
                                departmentAfter.setPhone(employee.getPhone());
//                                departmentAfter.setNumber(departmentAfter.getNumber() + 1);
                                departmentMapper.updateById(departmentAfter);
//                                employee.setDepartmentId(null);
                                return employeeMapper.updateById(employee) > 0;
                            }
                        } else {
                            //调到别的部门当部门副经理 部门主管 部门副主管，判断是否存在
                            System.out.println("调到别的部门当部门副经理 部门主管 部门副主管");
                            LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                            LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                            lambdaQueryWrapper1.eq(Department::getId,departmentId);
                            Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);
                            if(department1!=null){
                                lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                            }
                            lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                            lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                            Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                            if (employeeQuery != null) {//该部门的该职位存在，返回false
                                return false;
                            } else { //该部门的该职位不存在，需要修改该部门的人数加一，原先部门人数减一且修改为暂无
//                                departmentBefor.setEmployeeId(null);
//                                departmentBefor.setManager("暂无");
//                                departmentBefor.setPhone("暂无");
//                                departmentBefor.setNumber(departmentBefor.getNumber() - 1);
//                                departmentMapper.updateById(departmentBefor);
                                Department department = departmentService.lambdaQuery().eq(Department::getId,departmentBefor.getId()).one();
                                if(department!=null){
                                    System.out.println("department是"+department);
                                    departmentService.update(new UpdateWrapper<Department>().lambda()
                                            .set(Department::getPhone,"暂无")
                                            .set(Department::getEmployeeId,null)
                                            .eq(Department::getId,department.getId()));
                                    System.out.println("departmentBefor是"+departmentService.getById(departmentBefor.getId()));
                                }
//                                if(departmentAfter!=null){
//                                    departmentAfter.setNumber(departmentAfter.getNumber() + 1);
//                                }
//                                departmentMapper.updateById(departmentAfter);
                                return employeeMapper.updateById(employee) > 0;
                            }
                        }
                    }
                }
            }
        }else { //更改前不是部门经理
            System.out.println("更改前不是部门经理");
            if (positionOld.equals("员工")){ //更改前为员工
                System.out.println("更改前为员工");
                if (position.getPname().equals("总经理")
                        || position.getPname().equals("副总经理")
                        || position.getPname().equals("总监")
                        || position.getPname().equals("副总监")){
                //更改后为总经理 副总经理 总监 副总监(升职) 需判断是否存在
                    System.out.println("更改后为总经理 副总经理 总监 副总监(升职) ");
                    LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper1.eq(Department::getId,departmentId);
                    //更改后的部门id(这里一定是总部0，前端才能选中这四个职位，到达这个分支)
                    Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);//getByName
                    lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                    lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                    lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                    Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                    if (employeeQuery != null) {//该职位存在，返回false
                        return false;
                    }else { //该职位不存在，原部门人数减一，修改员工信息
//                        departmentBefor.setNumber(departmentBefor.getNumber()-1);
//                        departmentMapper.updateById(departmentBefor);
                        return employeeMapper.updateById(employee) > 0;
                    }
                }
                if (departmentId.equals(departmentOld)){
                //升职为本部门的部门经理
                    if (position.getPname().equals("部门经理")){
                        System.out.println("升职为本部门的部门经理");
                        //升职为本部门的部门经理，判断该部门是否存在部门经理
                        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper1.eq(Department::getId,departmentId);
                        Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);//getByName
                        lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                        lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                        lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                        Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                        if (employeeQuery != null) {//该部门的部门经理存在，返回false
                            return false;
                        }else { //该部门的部门经理不存在
                            departmentBefor.setEmployeeId(employee.getId());
                            departmentBefor.setPhone(employee.getPhone());
                            departmentMapper.updateById(departmentBefor);
                            return employeeMapper.updateById(employee) > 0;
                        }
                    }else {
                        System.out.println("升职为本部门的部门副经理 部门主管 部门副主管");
                        //升职为本部门的部门副经理 部门主管 部门副主管，判断是否已存在
                        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper1.eq(Department::getId,departmentId);
                        Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);//getByName
                        lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                        lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                        lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                        Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                        if (employeeQuery != null) {//该部门的该职位存在，返回false
                            return false;
                        }else { //该部门的该职位不存在
                            return employeeMapper.updateById(employee) > 0;
                        }
                    }
                }else { //员工调到别的部门
                    if (position.getPname().equals("员工")){ //调到别的部门当员工
                        System.out.println("调到别的部门当员工");
//                        departmentAfter.setNumber(departmentAfter.getNumber()+1);
//                        departmentBefor.setNumber(departmentBefor.getNumber()-1);
//                        departmentMapper.updateById(departmentAfter);
//                        departmentMapper.updateById(departmentBefor);
                        return employeeMapper.updateById(employee) > 0;
                    }
                    if (position.getPname().equals("部门经理")){ //调到别的部门当部门经理
                        System.out.println("升职调到别的部门当部门经理");
                        //升职调到别的部门当部门经理，判断该部门是否存在部门经理
                        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper1.eq(Department::getId,departmentId);
                        Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);
                        lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                        lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                        lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                        Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                        if (employeeQuery != null) {//该部门的部门经理已存在，返回false
                            return false;
                        }else { //该部门的部门经理不存在， 该部门人数加一，原部门人数减一，修改负责人和联系电话
                            System.out.println("调到别的部门当部门经理,该部门的部门经理不存在");
//                            departmentBefor.setEmployeeId(null);
//                            departmentBefor.setPhone("暂无");
//                            System.out.println("departmentBefor是什么"+departmentBefor);
//                                departmentBefor.setNumber(departmentBefor.getNumber() - 1);
//                            departmentMapper.updateById(departmentBefor);

                            Department department = departmentService.lambdaQuery().eq(Department::getId,departmentBefor.getId()).one();
                            if(department!=null){
                                System.out.println("department是"+department);
                                departmentService.update(new UpdateWrapper<Department>().lambda()
                                        .set(Department::getPhone,"暂无")
                                        .set(Department::getEmployeeId,null)
                                        .eq(Department::getId,department.getId()));
                                System.out.println("departmentBefor是"+departmentService.getById(departmentBefor.getId()));
                            }

                            departmentAfter.setEmployeeId(employee.getId());
                            departmentAfter.setPhone(employee.getPhone());//部门手机号修改成负责人的手机号
//                            departmentAfter.setNumber(departmentAfter.getNumber()+1);
                            departmentMapper.updateById(departmentAfter);
//                            departmentBefor.setNumber(departmentBefor.getNumber()-1);
//                            departmentMapper.updateById(departmentBefor);
                            return employeeMapper.updateById(employee) > 0;
                        }
                    }else {
                        //升职为别的部门的部门副经理 部门主管 部门副主管，判断是否存在
                        System.out.println("升职为别的部门的部门副经理 部门主管 部门副主管");
                        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper1.eq(Department::getId,departmentId);
                        Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);
                        lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                        lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                        lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                        Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                        if (employeeQuery != null) {//该部门的该职位存在，返回false
                            return false;
                        }else {
                            System.out.println("升职为别的部门的部门副经理 部门主管 部门副主管,该部门的该职位不存在");
                            //该部门的该职位不存在，该部门人数加一，原部门人数减一
//                            departmentAfter.setNumber(departmentAfter.getNumber()+1);
//                            departmentMapper.updateById(departmentAfter);
//                            departmentBefor.setNumber(departmentBefor.getNumber()-1);
//                            departmentMapper.updateById(departmentBefor);
                            return employeeMapper.updateById(employee) > 0;
                        }
                    }
                }
            }else { //更改前为部门副经理 部门主管 部门副主管
                if (departmentId.equals(departmentOld)){ //如果还是本部门员工
                    if (position.getPname().equals("员工")){
                        System.out.println("更改后为本部门员工");
                        //更改后为本部门员工
                        return employeeMapper.updateById(employee) > 0;
                    }
                    if (position.getPname().equals("部门经理")){
                        System.out.println("升职为本部门的部门经理，判断该部门是否存在部门经理");
                        //升职为本部门的部门经理，判断该部门是否存在部门经理
                        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        //---------------------------------------
                        LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper1.eq(Department::getId,departmentId);
                        Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);//getByName
                        lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                        lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                        lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                        Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                        if (employeeQuery != null) {//该部门的部门经理存在，返回false
                            return false;
                        }else { //该部门的部门经理不存在
                            System.out.println("升职为本部门的部门经理，该部门的部门经理不存在");
                            departmentBefor.setEmployeeId(employee.getId());
                            departmentBefor.setPhone(employee.getPhone());
                            return employeeMapper.updateById(employee) > 0;
                        }
                    }else {
                        System.out.println("更改为本部门的部门副经理 部门主管 部门副主管，判断是否存在");
                        //更改为本部门的部门副经理 部门主管 部门副主管，判断是否存在
                        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        //----------------------------------------
                        LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper1.eq(Department::getId,departmentId);
                        Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);//getByName
                        lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                        lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                        lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                        Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                        if (employeeQuery != null) {//该部门的该职位存在，返回false
                            return false;
                        }else { //该部门的该职位不存在
                            return employeeMapper.updateById(employee) > 0;
                        }
                    }

                }else { //该部门的部门副经理 部门主管 部门副主管调到别的部门
                    if (position.getPname().equals("部门经理")){
                        System.out.println("升职为别的部门的部门经理，判断该部门是否存在部门经理");
                        //升职为该部门的部门经理，判断该部门是否存在部门经理
                        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper1.eq(Department::getId,departmentId);
                        Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);
                        lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                        lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                        lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                        Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                        if (employeeQuery != null) {//该部门的部门经理存在，返回false
                            return false;
                        }else { //该部门的部门经理不存在， 该部门人数加一，原先部门人数减一，修改负责人和联系电话
                            System.out.println("升职为别的部门的部门经理，该部门的部门经理不存在");
                            departmentAfter.setEmployeeId(employee.getId());
                            departmentAfter.setPhone(employee.getPhone());
//                            departmentAfter.setNumber(departmentAfter.getNumber()+1);
                            departmentMapper.updateById(departmentAfter);
//                            departmentBefor.setNumber(departmentBefor.getNumber()-1);
//                            departmentMapper.updateById(departmentBefor);
                            return employeeMapper.updateById(employee) > 0;
                        }
                    }else {
                        System.out.println("到别的部门当部门副经理 部门主管 部门副主管，判断是否存在");
                        //到别的部门当部门副经理 部门主管 部门副主管，判断是否存在
                        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        //----------------------------------------------
                        LambdaQueryWrapper<Department> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper1.eq(Department::getId,departmentId);
                        Department department1 = departmentMapper.selectOne(lambdaQueryWrapper1);//getByName
                        lambdaQueryWrapper.eq(Employee::getDepartmentId, department1.getId());
                        lambdaQueryWrapper.eq(Employee::getPositionId,position.getId());
                        lambdaQueryWrapper.eq(Employee::getStatus,"正式");
                        Employee employeeQuery = employeeMapper.selectOne(lambdaQueryWrapper);
                        if (employeeQuery != null) {//该部门的该职位存在，返回false
                            return false;
                        }else {
                            System.out.println("到别的部门当部门副经理 部门主管 部门副主管，该部门的该职位不存在");
                            //该部门的该职位不存在，该部门人数加一，原部门人数减一
//                            departmentAfter.setNumber(departmentAfter.getNumber()+1);
//                            departmentMapper.updateById(departmentAfter);
//                            departmentBefor.setNumber(departmentBefor.getNumber()-1);
//                            departmentMapper.updateById(departmentBefor);
                            return employeeMapper.updateById(employee) > 0;
                        }
                    }
                }
            }
        }
    }


    /**
     * 同意入职申请
     * @param id 员工id
     * @param adminId 管理员id
     */
    @Override
    public void agreed(Integer id,Integer adminId) {
        Employee employee = employeeService.getById(id);
        employee.setStatus("实习");
        long d = System.currentTimeMillis();//系统时间
        Date date = new Date(d);
        employee.setWorktime(date);
        Admin admin = adminMapper.selectById(adminId);
        employee.setApprover(admin.getUsername());
        employeeMapper.updateById(employee);
        Department department = departmentMapper.selectById(employee.getDepartmentId());
        //不是必要的//点开浏览部门页，会自动计算
        department.setNumber(department.getNumber()+1);//入职后对应部门人数+1
        departmentMapper.updateById(department);//更新到数据库
    }

    /***
     * 驳回入职申请
     * @param id 员工id
     */
    @Override
    public void refuse(Integer id) {
        employeeMapper.deleteById(id);
    }

    /**
     * 员工修改个人信息
     * @param employee 员工信息
     */
    @Override
    public void updateSelfInfo(Employee employee) {
        employeeMapper.updateById(employee);
    }

}
